/**
 * @file    app_diagnosis.c
 * @brief   用户任务-故障诊断
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-05-03
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-30 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "app_diagnosis.h"
#include "FreeRTOS.h"
#include "task.h"
#include "diagnosis.h"
#include "diag_curr.h"
#include "diag_sys.h"
#include "diag_volt.h"
#include "diag_temp.h"
#include "param_mng.h"
#include "app_monitor.h"
#include "If_wdg.h"
#include "parallel.h"
#include "If_do.h"
#include "var_mng.h"
#include "heat_mng.h"
/* macro/define --------------------------------------------------------------*/
#define APP_DIAGNOSIS_TASK_CYCLE             (50) 
#define TOOL_OFF_DELAY                       (900000)  /* 15min */
#define CALIB_OFF_DELAY                      (600000)  /* 15min */
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
/* static variables ----------------------------------------------------------*/
static uint32_t tool_off_cnt = 0;
static uint32_t cailb_off_cnt = 0;
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief 诊断任务
*/
void vDiagnosisTask(void *pvParameters)
{
	uint32_t task_cycle = 0;
    portTickType xLastExecutionTime;

    xLastExecutionTime = xTaskGetTickCount();

	heat_mng_init();
	
	task_run_reset(TASK_ID_DIAGNOSIS);
	
    while(1)
    {
		task_cycle = task_get_cycle(TASK_ID_DIAGNOSIS);
		if(task_cycle > 20 * APP_DIAGNOSIS_TASK_CYCLE)
			task_cycle = APP_DIAGNOSIS_TASK_CYCLE;
		
		param_valid_check(PARAM_CHECK);
		
		diag_curr_occ(task_cycle);
		diag_curr_ocd(task_cycle);
        
		diag_volt_cov(task_cycle);
		diag_volt_cuv(task_cycle);
		diag_volt_bov(task_cycle);
		diag_volt_buv(task_cycle);
		diag_volt_vdiff(task_cycle);
        
		diag_temp_otc(task_cycle);
		diag_temp_utc(task_cycle);
		diag_temp_otd(task_cycle);
		diag_temp_utd(task_cycle);
		diag_temp_ote(task_cycle);
		diag_temp_ute(task_cycle);
		diag_temp_otm(task_cycle);
        // diag_temp_tdiff(task_cycle);

        diag_sys_ft_mos(task_cycle);
		diag_sys_col(task_cycle);
		diag_sys_tol(task_cycle);
		diag_sys_sc(task_cycle);
		diag_soc_alm(task_cycle); 
		// 并机PCS数据处理
		parallel_pcs_deal();
		
		if(TOOL_MODE_DEBUG != var_get_data(VAR_ID_TOOL_MODE))
		{
			if(diag_get_alarm(ALM_ID_SOCL))
				If_do_set_output(DO_RLY_SOCL , DO_OUT_ON);
			else
				If_do_set_output(DO_RLY_SOCL , DO_OUT_OFF);
			
			if(diag_get_prp(PRP_ID_COL) || diag_get_prp(PRP_ID_TOL) 
				|| diag_get_prp(PRP_ID_FT_CMOS) || diag_get_prp(PRP_ID_FT_DMOS)
				|| diag_get_prp(PRP_ID_FT_AFE))
				If_do_set_output(DO_RLY_FAULT , DO_OUT_ON);
			else
				If_do_set_output(DO_RLY_FAULT , DO_OUT_OFF);
		        heat_mng_process(task_cycle);	
        }
		else
		{
			if(var_get_data(VAR_ID_OUT_CTRL) & 0x01)
				If_do_set_output(DO_RLY_SOCL , DO_OUT_ON);
			else
				If_do_set_output(DO_RLY_SOCL , DO_OUT_OFF);
			
			if(var_get_data(VAR_ID_OUT_CTRL) & 0x02)
				If_do_set_output(DO_RLY_FAULT , DO_OUT_ON);
			else
				If_do_set_output(DO_RLY_FAULT , DO_OUT_OFF);
		}
        
		// 延时诊断清除 调试模式
		if(TOOL_MODE_DEBUG == var_get_data(VAR_ID_TOOL_MODE))
		{
			tool_off_cnt += task_cycle;
			if(tool_off_cnt >= TOOL_OFF_DELAY)
			{
				tool_off_cnt = 0;
				var_set_data(VAR_ID_TOOL_MODE , 0);
				var_set_data(VAR_ID_OUT_CTRL , 0);
				var_set_data(VAR_ID_MOS_CTRL , 0);
				var_set_data(VAR_ID_LED_SW , 0);
			}
		}
		else
		{
			tool_off_cnt = 0;
		}
		// 延时关闭校准模式
		if(CALIB_CMD_EN == var_get_data(VAR_ID_CALIB_CMD)) 
		{
			cailb_off_cnt += task_cycle;
			if(cailb_off_cnt >= TOOL_OFF_DELAY)
			{
				var_set_data(VAR_ID_CALIB_CMD , 0);
				cailb_off_cnt = 0;
			}
		}
		else
		{
			cailb_off_cnt = 0;
		}
		
        task_run_count(TASK_ID_DIAGNOSIS);
        
        vTaskDelayUntil( &xLastExecutionTime, APP_DIAGNOSIS_TASK_CYCLE);
    }
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


